package cn.edu.buaa.nlsde.grid.DatabaseConnect;

import java.sql.*;

public class DBInitial
{
	private static String Driver="org.gjt.mm.mysql.Driver";
	private static String server="localhost";       
	private static String dbname="ClusterMonitor";            
	private static String user="root";               
	private static String password="admin";           
	private static String port="3306";    
	private static String url ="jdbc:mysql://"+server+":"+port+"/"+dbname+"?user="+user+"&password="+password+"&useUnicode=true&characterEncoding=GB2312";
	private static String jndiName="java:comp/env/jdbc/MySQL";
	
	public void setDriver(String Driver)
	{
		this.Driver=Driver;
	}
	public void setServer(String server)
	{
		this.server=server;
	}
	public void setDBName(String dbname)
	{
		this.dbname=dbname;
	}
	public void setUser(String user)
	{
		this.user=user;
	}
	public void setPassword(String password)
	{
		this.password=password;
	}
	public void setJndiName(String jndiName)
	{
		this.jndiName=jndiName;
	}
	
	public static String getDriver()
	{
		return Driver;
	}
	public static String getURL()
	{
		return url;
	}
	public static String getJndiName()
	{
		return jndiName;
	}
	
	public static void createTable() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException
	{
		DBConn db=new DBConn(Driver,url);
		
		Statement DB=db.connect();
		String sql="drop table if exists Grid_Config";
		DB.executeUpdate(sql);
		
		DB=db.connect();
		sql="drop table if exists Cluster_Config";
		DB.executeUpdate(sql);
		
		DB=db.connect();
		sql="drop table if exists Host_Config";
		DB.executeUpdate(sql);
		
		DB=db.connect();
		sql="drop table if exists GlobalMapping";
		DB.executeUpdate(sql);
		
		DB=db.connect();
		sql="create table if not exists User_Info (UserID varchar(15) NOT NULL,UserName varchar(20),Pass_Word varchar(20),PRIMARY KEY(UserID))";
		DB.executeUpdate(sql);
		
		DB=db.connect();
		sql="create table if not exists Grid_Config (GridID int(11) DEFAULT 0,GridName varchar(20) NOT NULL,Authority varchar(50) NOT NULL,Local_Time DateTime,GridDesc varchar(100),PRIMARY KEY(GridID))";
		DB.executeUpdate(sql);
		
		DB=db.connect();
		sql="create table if not exists Cluster_Config (ClusterID int(11) DEFAULT 0,ClusterName varchar(20) NOT NULL,Local_Time DateTime,Owner varchar(15) NOT NULL,Latlong varchar(15) NOT NULL,URL varchar(20) NOT NULL,ClusterDesc varchar(100),PRIMARY KEY(ClusterID))";
		DB.executeUpdate(sql);
		
		DB=db.connect();
		sql="create table if not exists Host_Config (HostID int(11) DEFAULT 0,HostName varchar(20) NOT NULL,HostIP varchar(15) NOT NULL,Reported DateTime,Location varchar(15) NOT NULL,GMOND_STARTED DateTime,PRIMARY KEY(HostID))";
		DB.executeUpdate(sql);
		
		DB=db.connect();
		sql="create table if not exists NodeAlter (ClusterID int(11) DEFAULT 0,HostID int(11) DEFAULT 0,HostStatus varchar(5) NOT NULL,PRIMARY KEY(ClusterID,HostID,HostStatus))";
		DB.executeUpdate(sql);
		
		DB=db.connect();
		sql="create table if not exists GlobalMapping (GridID int(11),ClusterID int(11),HostID int(11),PRIMARY KEY(GridID,ClusterID,HostID))";
		DB.executeUpdate(sql);
		
		DB=db.connect();
		sql="create table if not exists Metric_System (HostID int(11) DEFAULT 0,machine_type varchar(10),boottime DateTime,cpu_num int(11),cpu_speed int(11),os_name varchar(20),os_release varchar(20),gexec varchar(10),part_max_used float,CollectTime DateTime,PRIMARY KEY(HostID,CollectTime))";
		DB.executeUpdate(sql);
		
		DB=db.connect();
		sql="create table if not exists Metric_CPU (HostID int(11) DEFAULT 0,cpu_nice float,cpu_wio float,cpu_aidle float,cpu_idle float,cpu_user float,cpu_system float,CollectTime DateTime,PRIMARY KEY(HostID,CollectTime))";
		DB.executeUpdate(sql);
		
		DB=db.connect();
		sql="create table if not exists Metric_Memory (HostID int(11) DEFAULT 0,swap_total int(11),swap_free int(11),mem_total int(11),mem_free int(11),mem_cached int(11),mem_buffers int(11),mem_shared int(11),CollectTime DateTime,PRIMARY KEY(HostID,CollectTime))";
		DB.executeUpdate(sql);
		
		DB=db.connect();
		sql="create table if not exists Metric_Process (HostID int(11) DEFAULT 0,proc_total int(11),proc_run int(11),CollectTime DateTime,PRIMARY KEY(HostID,CollectTime))";
		DB.executeUpdate(sql);
		
		DB=db.connect();
		sql="create table if not exists Metric_Load (HostID int(11) DEFAULT 0,load_one float,load_five float,load_fifteen float,CollectTime DateTime,PRIMARY KEY(HostID,CollectTime))";
		DB.executeUpdate(sql);
		
		DB=db.connect();
		sql="create table if not exists Metric_PKTS (HostID int(11) DEFAULT 0,bytes_in float,bytes_out float,pkts_in float,pkts_out float,CollectTime DateTime,PRIMARY KEY(HostID,CollectTime))";
		DB.executeUpdate(sql);
		
		DB=db.connect();
		sql="create table if not exists Metric_Disk (HostID int(11) DEFAULT 0,disk_total double,disk_free double,CollectTime DateTime,PRIMARY KEY(HostID,CollectTime))";
		DB.executeUpdate(sql);
		
		db.close();
	}
}